<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>mysql入门介绍 | 辛巴达的宝藏</title>
    <meta name="generator" content="VuePress 1.5.0">
    <link rel="icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">
    <script src="/blog/live2d-widget/autoload.js"></script>
    <meta name="description" content="想要我的宝藏吗，快来吧，就在这里">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/blog/assets/css/0.styles.5ed5bb7b.css" as="style"><link rel="preload" href="/blog/assets/js/app.57203e40.js" as="script"><link rel="preload" href="/blog/assets/js/3.bdb1e6ab.js" as="script"><link rel="preload" href="/blog/assets/js/1.2d82282b.js" as="script"><link rel="preload" href="/blog/assets/js/18.d303b720.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.ecfbeae8.js"><link rel="prefetch" href="/blog/assets/js/11.4fd3eac0.js"><link rel="prefetch" href="/blog/assets/js/12.dc6db768.js"><link rel="prefetch" href="/blog/assets/js/13.99399268.js"><link rel="prefetch" href="/blog/assets/js/14.3dda668c.js"><link rel="prefetch" href="/blog/assets/js/15.7666d17c.js"><link rel="prefetch" href="/blog/assets/js/16.6cd0c6fb.js"><link rel="prefetch" href="/blog/assets/js/17.718dda6e.js"><link rel="prefetch" href="/blog/assets/js/19.f2513224.js"><link rel="prefetch" href="/blog/assets/js/20.bc38c8bc.js"><link rel="prefetch" href="/blog/assets/js/4.2457f4da.js"><link rel="prefetch" href="/blog/assets/js/5.e2e76e40.js"><link rel="prefetch" href="/blog/assets/js/6.00aabcc1.js"><link rel="prefetch" href="/blog/assets/js/7.de697a5f.js"><link rel="prefetch" href="/blog/assets/js/8.c5210b45.js"><link rel="prefetch" href="/blog/assets/js/9.02c22cc6.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.5ed5bb7b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-dad8a512><div data-v-dad8a512><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-dad8a512 data-v-dad8a512><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-dad8a512 data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>辛巴达的宝藏</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>辛巴达</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-dad8a512><header class="navbar" data-v-dad8a512><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.png" alt="辛巴达的宝藏" class="logo"> <span class="site-name">辛巴达的宝藏</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/http/" class="nav-link"><i class="iconfont undefined"></i>
  http
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时光轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/znvoid" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-dad8a512></div> <aside class="sidebar" data-v-dad8a512><div class="personal-info-wrapper" data-v-ca798c94 data-v-dad8a512><img src="/blog/avatar.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    辛巴达
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>9</h3> <h6 data-v-ca798c94>Article</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>10</h3> <h6 data-v-ca798c94>Tag</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/java/" class="nav-link"><i class="iconfont undefined"></i>
  java
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/spring/" class="nav-link"><i class="iconfont undefined"></i>
  spring
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/http/" class="nav-link"><i class="iconfont undefined"></i>
  http
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时光轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/znvoid" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-dad8a512><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>mysql入门介绍</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>辛巴达</span>
            
          <span data-v-64685f0e>2020 - </span>
          2022
        </a></span></div></div> <div data-v-dad8a512><main class="page"><div class="page-title" style="display:none;"><h1>mysql入门介绍</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>辛巴达</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-09-12</span></i> <!----> <i class="iconfont reco-tag tags" data-v-3b7f5bdf><span class="tag-item" data-v-3b7f5bdf>mysql</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="mysql"><a href="#mysql" class="header-anchor">#</a> Mysql</h1> <h3 id="基本概念"><a href="#基本概念" class="header-anchor">#</a> 基本概念</h3> <div class="language- extra-class"><pre><code>是一个文件系统,存储数据,通过标准的SQLy语句获取数据
</code></pre></div><h4 id="关系型数据库"><a href="#关系型数据库" class="header-anchor">#</a> 关系型数据库</h4> <div class="language- extra-class"><pre><code>关系型数据库存放的是实体之间的关系  常见关系型如:mysql orcale db2
</code></pre></div><h5 id="mysql-安装"><a href="#mysql-安装" class="header-anchor">#</a> mysql 安装</h5> <div class="language- extra-class"><pre><code>注意路径不能有空格 中文等
</code></pre></div><h3 id="数据存储方式"><a href="#数据存储方式" class="header-anchor">#</a> 数据存储方式</h3> <div class="language- extra-class"><pre><code>数据库服务器 --&gt;数据库 ---&gt;表(为每个实体创建一个表)--&gt;字段

一台数据库服务器中会创建很多数据库（一个项目，会创建一个数据库）。在数据库中会创建很多张表（一个实体会创建一个表）。在表中会有很多记录（一个对象的实例会添加一条新的记录）。
</code></pre></div><div class="language- line-numbers-mode"><pre class="language-text"><code>graph TB
数据库服务器--&gt;数据库1
数据库服务器--&gt;数据库2
数据库1--&gt;表1
数据库1--&gt;表2

数据库2--&gt;表3
数据库2--&gt;表4
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language- extra-class"><pre><code>   eg:表结构(table)
</code></pre></div><table><thead><tr><th>id</th> <th>name</th> <th>age</th></tr></thead> <tbody><tr><td>1</td> <td>张三</td> <td>13</td></tr> <tr><td>2</td> <td>李四</td> <td>15</td></tr></tbody></table> <div class="language- extra-class"><pre><code>netstat -ano    端口查看
tasklist /svc   任务列表
ipconfig /flushdns  ip刷新
</code></pre></div><h2 id="sql-结构化查询语言"><a href="#sql-结构化查询语言" class="header-anchor">#</a> sql 结构化查询语言</h2> <p><strong>分类:</strong></p> <div class="language- extra-class"><pre><code>    DDL 数据定义语言    数据库/表的操作
        create drop altet
    DCL 数据控制语言
        grant if
    DML 数据操作语言        对数据进行操作
        insert updata delete
    DQL 数据查询语言
        select
</code></pre></div><p><strong>数据库操作</strong></p> <div class="language- extra-class"><pre><code>创建数据库	CREATE DATABASE 数据库名称 CHARACTER SET 字符集 COLLATE; 字符集校对规则
    eg:
       CREATE DATABASE; //字符集和校对规则可省略 

查看数据库 SHOW  DATABASES; 
    
查看某个数据库的定义信息： SHOW CREATE DATABASE 数据库名称;

修改数据库    
    修改数据库字符集
        ALTER 数据库名 CHARACTER SET 字符集;
    删除数据库
        DROP DATABASE 数据库名;
切换使用的数据库    use 数据库名;
查看正在使用的数据库    select database();
</code></pre></div><p><strong>数据类型</strong></p> <div class="language- extra-class"><pre><code>表中字段类型
Java中的类型						MySQL中的类型
    byte/short/int/long 				tinyint/smallint/int/bigint
    float								float
    double								double
    boolean								bit
    char/String							char和varchar类型   //需要指定长度
    Date								date/time/datetime/timestamp
    File                                blob/text   //很少用

注意:在mysql中
</code></pre></div><ul><li><div class="language- extra-class"><pre><code>    char代表是固定长度的字符或字符串   eg:char(8) 存入长度不够8用空格不齐
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    varchar代表的是可变长度的字符串 eg: varchar(8)    长度不够8不会不齐   
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    datetime就是既有日期又有时间的日期类型,如果没有向这个字段中存值，数据库使用null存入到数据库中
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    timestamp也是既有日期又有时间的日期类型，如果没有向这个字段中存值，数据库使用当前的系统时间存入到数据库中
</code></pre></div></li></ul> <p><strong>表的操作</strong></p> <blockquote><div class="language- extra-class"><pre><code>创建表      CREATE TABLE 表名(字段名称 字段类型(长度) 约束,字段名称 字段类型(长度) 约束...);
eg:
    CREATE TABLE person{
        id INT PRIMARY KEY AUTO_INCREMENT,
        name VARCHAR(10) UNIQUE,
        age INT
    };
注意:
    创建表之前要有数据库并使用数据库
    约束可省略
约束
    主键    PRIMARY KEY 默认非空唯一
    非空    NOT NULL
    唯一    UNIQUE

查看所有表
    SHOW TABLES;
查看某个表的结构信息
    DESC 表名;
查看表创建信息
    SHOW CREATE TABLE 表名;
删除表
   DROP TABLE 表名;
</code></pre></div></blockquote> <blockquote><div class="language- extra-class"><pre><code>修改表

    1）添加列
	ALTER TABLE 表名称 ADD 列名 类型(长度) 约束;
	eg:
		ALTER TABLE t_address ADD phone VARCHAR(20);

	2)修改列的类型、长度、约束
	ALTER TABLE 表名称 MODIFY  类型(长度) 约束;
	eg：
		ALTER TABLE t_address MODIFY phone VARCHAR(30) NOT NULL;

	3)修改列名
	ALTER TABLE 表名称 CHANGE 旧列名 新列名 类型(长度) 约束;
	eg：
		ALTER TABLE t_address CHANGE phone tel VARCHAR(30) NOT NULL;

	4)删除列
	ALTER TABLE 表名称 DROP 列名;
	eg:
		ALTER TABLE t_address DROP tel;
  5)修改表名

      RENAME TABLE 表名 TO 新表名;	
</code></pre></div></blockquote> <ul><li><div class="language- extra-class"><pre><code>  插入数据
  
  1)INSERT INTO 表名(列名1,列名2,...,列名N) VALUES(值1,值2,...,值N);
  2)INSERT INTO 表名(列名1,列名2,...,列名N) VALUES(值1,值2,...,值N),(值1,值2,...,值N),...,(值1,值2,...,值N);
  3)INSERT INTO 表名 VALUES(值1,值2,...,值N);
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>  更新数据
  
  UPDATE 表名 SET 列名1=值1,列名2=值2,...,列名n=值n WHERE 条件;
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>  删除数据

  1)删除部分数据
  	DELETE FROM 表名 WHERE 条件;
  
  2)删除表内所有数据
  
  	--DELETE是一条条删除，不会清空AUTO_INCREMENT
  	DELETE FROM 表名;   --属于DML,事务可以作用在DML上
  
  	--直接将表删除，重建，AUTO_INCREMENT重置为0
  	TRUNCATE TABLE 表名;    --属于DDL
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>  查询数据
     基本查询
          SELECT * FROM 表名 ;    --查询所有
          SELECT 列名1,列名2... FROM 表名 ;     --查询指定列数据
          SELECT DISTINCT 列名1... FROM 表名  ;    --去重查询,参数所有列完全相同时去重
          SELECT 列名1 AS 列名1别名,列名2 AS 列名2别名 FROM 表名 ;    --别名查询,AS可以省略
      条件查询
          基本查询+WHERE 条件;
          SELECT * FROM 表名 WHERE 条件;
          条件:
              &gt;, &lt; ,&gt;=, &lt;= ,&lt;&gt;, =
              like 模糊查询,使用_或%作为站位符,_代表一个字符,%代表任意个字符
              in      范围查询 匹配内容
              between .. and
              and ,or  ,not
          
          eg:
          
          SELECT * FROM headset WHERE name LIKE '李_';
          SELECT * FROM headset WHERE age IN(21,22,23);
          SELECT * FROM headset WHERE money BETWEEN 1000 AND 1800;
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>       排序查询
          order by 字段名 asc(升序默认可省略)/desc(降序);
          eg:
              select * from exam order by score;
              select * from exam order by score desc;
              select * from exam order by score desc,age asc;
      分页查询 sql语句最后
          limit 起始index,数目
      
      聚合函数
          count
          max/min
          sum
          avg
      eg:
          SELECT sum(english) from exam where name like '李%';

      ifnull(字段,默认值)     --当字段为空时以默认值计算
      
      分组查询
          SELECT 字段 ,COUNT(*) FROM 表名 GROUP BY 字段;
          
         聚合函数通常和分组配合,  where 后不能跟聚合函数 需要用having
          
          
          SELECT 字段 ,SUM(字段1) FROM 表名 GROUP BY 字段 HAVING 条件;
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>   总结
  	S(select)… F(from)…W(where)…G(group by)…H(having)…O(order by);
</code></pre></div></li></ul> <h2 id="多表"><a href="#多表" class="header-anchor">#</a> 多表</h2> <h4 id="约束"><a href="#约束" class="header-anchor">#</a> 约束</h4> <div class="language- extra-class"><pre><code>外键约束:保证数据的完整性

alter table 表 add foreign key(字段) references 表a(字段a)

外键一般连接的都是主键
</code></pre></div><h4 id="多表关系"><a href="#多表关系" class="header-anchor">#</a> 多表关系</h4> <div class="language- extra-class"><pre><code>    一对一
        一的其中一方创建外键指向另一方的主键
        主键绑定主键

    一对多
        多的一方创建外键指向一的主键
    多对多
        需要创建第三张表(中间表),在中间表中能够至少有两个字段作为外键分别指向多对多双方主键
</code></pre></div><h3 id="多表查询"><a href="#多表查询" class="header-anchor">#</a> 多表查询</h3> <p>连接查询</p> <div class="language- extra-class"><pre><code>交叉连接(了解)
    两个表的笛卡尔积
    select * from 表1 cross join 表2;
    select * from 表1 , 表2;
内连接
    显示内连接  inner join  (inner 可省略)
         select * from 表1 inner join 表2 on 关联条件;
    隐士内连接
         select * from 表1,表2 where 关联条件;
    
外连接  outer join (outer 可省略)
    左外连接
        select * from 表1 left outer join 表2 on 关联条件
    右外连接
        select * from 表1 right outer join 表2 on 关联条件
</code></pre></div><p>子查询</p> <div class="language- extra-class"><pre><code>一个查询语句需要依赖另一个查询语句的结果    查询中嵌套查询

in

any 
    任意一个
all 
    所有
exists  
    select * from clsses where exists(selct cno from student where birthday&gt;'1991-01-01')
</code></pre></div><p>###事务</p> <blockquote><p>特性</p></blockquote> <div class="language- extra-class"><pre><code>原子性
    事务不可分割
一致性
    事务执行的前后,数据的完整性保持一致
隔离性
    一个事务的执行不应该受到其他事务的干扰
持久性
    事务一旦提交结束 就因该保存到数据库中
</code></pre></div><blockquote><p>开启事务</p></blockquote> <div class="language- extra-class"><pre><code>    start transaction
提交事务
    commit
回滚事务
    rollback
</code></pre></div><blockquote><p>隔离级别</p></blockquote> <div class="language- extra-class"><pre><code>基于隔离性出现的问题
    脏读
        一个事务查询到另外一个事务未提交的数据,导致查询结果不一致
    不可重复读
        一个事务读到了到另外一个事务已提交的update数据,导致多次查询结果不一致
    虚读/幻读
     一个事务读到了到另外一个事务已提交的insert数据,导致多次查询结果不一致
级别
    read uncommitted
    read committed
    repeatable 
    serializable
</code></pre></div><hr> <h2 id="jdbc"><a href="#jdbc" class="header-anchor">#</a> JDBC</h2> <div class="language- extra-class"><pre><code>java数据连接  一组Java语言的类和接口  规范


驱动:两设备(应用)之间的通信桥梁
</code></pre></div><p>步骤</p> <ol><li><div class="language- extra-class"><pre><code>    注册驱动
     Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    获取连接对象
  Connection con=DriverManager.getConnection(&quot;jdbc:mysql://ip地址:端口/数据库名称&quot;,数据库用户名,数据库密码);
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    获得一个操作sql对象
Statement sm=  con.createStatement;
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    执行sql语句 获取结果
     String sql =&quot;sql语句&quot;;
     ResultSet set=sm.executeQuery(sql); 查询
     int row =sm.executeUpdate(sql);     增删改
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    处理结果
</code></pre></div></li> <li><div class="language- extra-class"><pre><code>    释放资源
     set.close();
     sm.close();
     con.close()
</code></pre></div><div class="language- line-numbers-mode"><pre class="language-text"><code>eg:
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException sqlEx) { // ignore }
    
                rs = null;
        }
        
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) { // ignore }
    
            stmt = null;
        }

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></li></ol> <p>JDBC 的sql 注入漏洞</p> <div class="language- extra-class"><pre><code>把用户输入得参数当作sql关键字被解释执行 
</code></pre></div><h2 id="jdbc事务"><a href="#jdbc事务" class="header-anchor">#</a> jdbc事务</h2> <ol><li><p>环境准备</p> <div class="language- extra-class"><pre><code> 数据库及表
 create database web_test;
 use web_test;
 create table account(
     id int primary key auto_increment,
     userName varchar(20),
     password varchar(20),
     money double
 );
 insert into account values(null,zhangsan,12345);
</code></pre></div></li> <li><p>开启事务
conn.setAutoCOmmit(false);//关闭自动提交</p></li> <li><p>提交事务
conn.commit();</p></li> <li><p>事务回滚
conn.rollback();</p></li></ol> <div class="language-Java 代码 line-numbers-mode"><pre class="language-java"><code>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="连接池"><a href="#连接池" class="header-anchor">#</a> 连接池</h2> <div class="language- extra-class"><pre><code>创建和管理一个连接的缓存池的技术
连接的创建和销毁需要时间,在服务器初级化时就初始化一些连接放入连接池中,使用时可从内存中获取,在内存中效率高
</code></pre></div><p>1.自定义连接池
装饰着模式</p> <p>2.Druid连接池</p> <div class="language- extra-class"><pre><code>DuridDataSource ds =new DuridDataSource();
//手动
ds.setDriverClassName(&quot;&quot;);
ds.setUrl(&quot;&quot;);
ds.setPassword(&quot;&quot;);
//配置文件方式
/*
    DriverClassName=
    url=
    username=
    password=
*/
Properties prop= new Properties()
prop.load(InputStream)
DruidDataSourceFaxtory.createDataSource(prop)

//获得连接
ds.getConnection();
</code></pre></div><p>3.c3p0连接池</p> <div class="language- extra-class"><pre><code>ComboPooledDataSource ds =new ComboPooledDataSource();
ds,setDriverClass(&quot;&quot;);
ds.setJdbcUrl(&quot;&quot;);
ds.setUser(&quot;&quot;);
ds.setPassword(&quot;&quot;);

//获得连接
 ds.getConnection();
</code></pre></div><ol start="4"><li><p>DBUtils</p> <p>对jdbc简单的封装而没有影响性能</p></li></ol></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">9/12/2020, 3:22:10 PM</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/blog/assets/js/app.57203e40.js" defer></script><script src="/blog/assets/js/3.bdb1e6ab.js" defer></script><script src="/blog/assets/js/1.2d82282b.js" defer></script><script src="/blog/assets/js/18.d303b720.js" defer></script>
  </body>
</html>
